<?php
	if (session_status() == PHP_SESSION_NONE) {
		function __autoload($class_name){
			$dirs = array(
				'../../Factories/',
				'../../cntrlr/',
				'../../mdl/',
				'../../mdl/Delete/',
				'../../mdl/Select/',
				'../../mdl/Update/',
				'../../mdl/Insert/',
				'../../'
			);
			foreach($dirs as $dir){
				if(file_exists($dir.$class_name . '.php')){
					require_once($dir.$class_name . '.php');
					return;
				}
			}
			throw new BadFunctionCallException("No such Object file");
		}

		session_start();
	}
?>
<div id='orderView'>
	<table id='tblorder'>
	<?php
		$array = $_SESSION["order"]->getStocks();

		foreach ($array as $stock){
			echo "<tr>
					<td>
						<img width='120px' height ='120px' src='images/products/".$stock->getImage()."' >
					</td>
					<td  id='description'>
						<h2 id='".$stock->getId()."'>".$stock->getName()."</h2><br/>

						<a href='?page=search&searchField='".$stock->getProducer()."''>
							".$stock->getProducer()."
						</a>
						<br/><br/><hr><br/>
						Минимално количество : ".$stock->getMinimumQuantity()."  ".$stock->getQuantity()."

					</td>
					<td width='40px'>
						<input type='text' value='".$stock->getOrderQuantity()."' class='".$stock->getId()."' id='".$stock->getId()."' style='width:30px;text-align: right;'>
					</td>
					<td width='60px'>";
			if ($stock->getPromoPrice() !=null){
				$price=$stock->getPromoPrice() ;
			} else {
				$price=$stock->getPrice();
			}
			printf("%.2f лв.",$price);
		echo		    "<hr>
						Ед. Цена

					</td>
					<td width='60px'>";

			printf("<h3> %.2f </h3> ",$price*$stock->getOrderQuantity());
		echo			"</td>
					<td width='60px' class='del'>
						<img id='".$stock->getId()."' src='images/delCart.gif' width='40px' height='40px'>
					</td>
				</tr>
				";
		}
	?>

			<tr >
				<td colspan='6' style='text-align: right;' >
					<h2> Сума : <?php printf(" %.2f лв. ", $_SESSION["order"]->getSum()); ?> </h2><br/>
				</td>
			</tr>
			<tr id='buy'>
				<td colspan='6' style='text-align: right;' >
					<input type='button' class='button' id="clear" value=' Изпразни Кошницата '>
					<?php
						if (isset($_SESSION["login"])){
							echo "<input type='button' class='button' id='order' value=' Поръчай '>";
						}
					?>


				</td>
			</tr>
	</table>

</div>
<script>
	$('.del img').click(function(){
		var id = $(this).attr("id");
		var  Quantity= $("."+id).val();
		var productName = $("h2#"+id).text();
		$.get("cntrlr/cart.php", { quantity: Quantity, delete : productName  })
			.done(function(data) {
				$("#cart").hide(500,function(){
					$(this).html(data).show(500);

				});
				$("#orderView").load("vw/userside/orderView.php");
			});

	});
	$('input[type=text]').keyup(function(){
		var id = $(this).attr("id");
		var  Quantity= $("."+id).val();
		var productName = $("h2#"+id).text();
		$.get("cntrlr/cart.php", { quantity: Quantity, update : productName  })
			.done(function(data) {
				$("#cart").hide(500,function(){
					$(this).html(data).show(500);

				});
				$("#orderView").load("vw/userside/orderView.php");

			});
	});
	$('#clear').click(function(){
		$.get("cntrlr/cart.php", { clear: true})
			.done(function(data) {
				$("#cart").hide(500,function(){
					$(this).html(data).show(500);

				});
				$("#orderView").html("КОШНИЦАТА Е ПРАЗНА");
			});
	});
	$('#order').click(function(){
		var id = $("input[name=shopname]:checked").val();
		var productName = $("h2#"+id).text();
		$.get("cntrlr/finishOrder.php", { shopId : id  })
			.done(function(data) {
				$("#main_content").html(data);
			});
	});
</script>
